/**
 *
 */
package com.onstar.cn.map.util;

import android.graphics.Point;
import android.util.Log;
import android.view.View;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.LatLngBounds;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.core.OGeoPoint;
import com.onstar.cn.map.core.impl.OGeoPointImpl;
import com.onstar.cn.map.route.RouteOverlay;

/**
 * @author Qiying Gong
 * @Created 2013-4-12
 */
public class MapViewHelper {

    private static MapViewHelper self = null;

    public static synchronized MapViewHelper getInstance() {
        if (self == null) {
            self = new MapViewHelper();
        }

        return self;
    }

    private MapViewHelper() {
    }

    public void clearMapOverlays(AMap mapView) {
        mapView.clear();
    }

    public void clearMapOverlay(AMap mapView) {
        this.removeAllViews(mapView);
    }

    /**
     * @param mapView
     */
    protected void removeAllViews(AMap mapView) {
        if (mapView != null) {
            mapView.clear();
        }
    }

    public void removeView(AMap mapView, View view) {
        // if (mapView != null && view != null) {
        // mapView.removeView(view);
        // }
    }

    public LatLonPoint getPointer(final double latit, final double longit) {
        LatLonPoint geoPoint;
        geoPoint = new LatLonPoint(latit, longit);

        return geoPoint;
    }

    public void zoomToSpan(AMap mapView, LatLngBounds box) {
        try {
//            mapView.moveCamera(CameraUpdateFactory.newLatLngBounds(box, 5));
            float level = 0;
            if (mapView.getCameraPosition() != null) {
                level = mapView.getCameraPosition().zoom - 1.5f;
            }
            Log.w("bounding box level:", "" + level);
            setZoom(mapView, level);
        } catch (java.lang.OutOfMemoryError e) {
            e.printStackTrace();
            return;
        }
    }

    public void setMapCenter(AMap mapView, LatLonPoint pt, float zoomLevel) {
        CameraPosition position = getCameraPosition(pt, zoomLevel);
        CameraUpdate center = CameraUpdateFactory.newCameraPosition(position);
        mapView.moveCamera(center);
    }

    /**
     * @param pt
     * @param zoomLevel
     * @return
     */
    private CameraPosition getCameraPosition(LatLonPoint pt, float zoomLevel) {
        CameraPosition position = new CameraPosition.Builder().target(new LatLng(pt.getLatitude(), pt.getLongitude()))
                .zoom(zoomLevel).build();
        return position;
    }

    public void animateTo(AMap mapView, LatLonPoint point) {
        if (mapView.getCameraPosition() == null) return;
        CameraPosition position = getCameraPosition(point, mapView.getCameraPosition().zoom);
        CameraUpdate camera = CameraUpdateFactory.newCameraPosition(position);
        mapView.animateCamera(camera);

    }

    // public void addOverlay(AMap mapView, Overlay overlay) {
    // mapView.getOverlays().add(overlay);
    // }

    public void setZoom(AMap mapView, float mapLevel) {
        CameraUpdate zoomLevel = CameraUpdateFactory.zoomTo(mapLevel + 0.0F);
        mapView.animateCamera(zoomLevel);
    }

    public OGeoPoint fromPixels(AMap mapView, int x, int y) {
        Point spt = new Point(x, y);
        LatLng pt = mapView.getProjection().fromScreenLocation(spt);
        OGeoPoint opt = new OGeoPointImpl(pt.latitude, pt.longitude);

        return opt;
    }

    public void clearRouteOverlay(AMap mapView, RouteOverlay routeOverlay) {
        if (routeOverlay != null) {
            routeOverlay.removeFromMap();
        }
    }

    public LatLng getLatLng(LatLonPoint pt) {
        return new LatLng(pt.getLatitude(), pt.getLongitude());
    }

    public LatLng getLatLng(double lat, double lon) {
        return new LatLng(lat, lon);
    }

    public Marker addOverlay(AMap mapView, MarkerOptions poiOverlay) {
        Marker marker = mapView.addMarker(poiOverlay);
        return marker;
    }

    public boolean isSamePoint(PoiItemInfo itemInfo1, PoiItemInfo itemInfo2) {
        if (itemInfo1 != null && itemInfo2 != null) {
            double distance = DistanceCalculation.calculateDistance(itemInfo1.getLongitude(), itemInfo1.getLatitude(),
                    itemInfo2.getLongitude(), itemInfo2.getLongitude());
            if (distance < 10) {
                return true;
            }
        }

        return false;
    }

    public void setCompassEnable(AMap map, boolean enable) {
        map.getUiSettings().setCompassEnabled(enable);
    }

    public void setZoomControlsEnabled(AMap mMapView, boolean enable) {
        mMapView.getUiSettings().setZoomControlsEnabled(true);
    }
}
